package com.zhiyou100.service.impl;


import com.zhiyou100.entity.Emp;
import com.zhiyou100.mapper.EmpMapper;
import com.zhiyou100.service.EmpService;
import org.apache.ibatis.io.Resources;
import org.apache.ibatis.session.SqlSession;
import org.apache.ibatis.session.SqlSessionFactory;
import org.apache.ibatis.session.SqlSessionFactoryBuilder;

import java.io.IOException;
import java.io.InputStream;
import java.util.HashMap;
import java.util.List;

public class EmpServiceImpl implements EmpService {


    @Override
    public List<Emp> getAllEmp() {
        System.out.println("EmpServiceImpl 1");
        InputStream resourceAsStream = null;
        try {
            System.out.println("EmpServiceImpl 2");
            resourceAsStream = Resources.getResourceAsStream("mybatis-config.xml");
        } catch (IOException e) {
            e.printStackTrace();
        }
        System.out.println("EmpServiceImpl 3");
        SqlSessionFactoryBuilder sqlSessionFactoryBuilder = new SqlSessionFactoryBuilder();

        SqlSessionFactory sf = sqlSessionFactoryBuilder.build(resourceAsStream);

        SqlSession sqlSession = sf.openSession();

        EmpMapper mapper = sqlSession.getMapper(EmpMapper.class);
        System.out.println("EmpServiceImpl 4");
        List<Emp> allEmp = mapper.getAllEmp();
        System.out.println("11111111111111111111111111111111");
        System.out.println(allEmp);
        sqlSession.close();
        return allEmp;
    }

    @Override
    public List<Emp> getEmpByMany(HashMap<String,Object> map) {
        InputStream resourceAsStream = null;
        try {
            resourceAsStream = Resources.getResourceAsStream("mybatis-config.xml");
        } catch (IOException e) {
            e.printStackTrace();
        }

        SqlSessionFactoryBuilder sqlSessionFactoryBuilder = new SqlSessionFactoryBuilder();

        SqlSessionFactory sf = sqlSessionFactoryBuilder.build(resourceAsStream);

        SqlSession sqlSession = sf.openSession();

        EmpMapper mapper = sqlSession.getMapper(EmpMapper.class);
        List<Emp> allEmp = mapper.getEmpByMany(map);
        sqlSession.close();
        return allEmp;
    }
}
